/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controle;

import Classes.Cliente;
import Classes.Veiculo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Rafael
 */
public class ControleVeiculo {

    Conexao cx;
    Connection con;

    public ControleVeiculo() throws SQLException {
        cx = new Conexao();
        con = cx.getConnection();
    }

    public void alteraEntradaSaida(String plv, String stv) throws SQLException {

        String querry = "UPDATE veiculo SET status_veiculo='" + stv + "' WHERE placa='" + plv +"'";
        PreparedStatement ps = con.prepareStatement(querry);
        ps.executeUpdate();
    }
    
    
    public void adicionaVeiculo(Veiculo v) throws SQLException {

        String cor = v.getCor();
        String placa = v.getPlaca();
        String modelo = v.getModelo();
        Integer codCliente = Integer.parseInt(v.getCod_cliente());
        Integer status_veiculo = Integer.parseInt(v.getStatus_veiculo());

        //Date dataCadastro;

        String querry = "INSERT INTO veiculo(cod_cliente, cor, placa, modelo, status_veiculo) "
                + "VALUES(?, ?, ?, ?, ?)";
        PreparedStatement ps = con.prepareStatement(querry);

        ps.setInt(1, codCliente);
        ps.setString(2, cor);
        ps.setString(3, placa);
        ps.setString(4, modelo);
        ps.setInt(5, status_veiculo);

        ps.executeUpdate();

    }

    public ArrayList listaVeiculosEstacionados() {
        ArrayList<Veiculo> veiculose = new ArrayList();
        Veiculo v;
        try {
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("SELECT * FROM veiculo WHERE status_veiculo = 1");
            while (rs.next()) {
                v = new Veiculo(rs.getString("placa"), rs.getString("modelo"),
                        rs.getString("cor"), rs.getString("status_veiculo"));
                veiculose.add(v);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return veiculose;
    }
    
    public ArrayList listaVeiculosCliente(String codCli) {
        ArrayList<Veiculo> veiculos = new ArrayList();
        Veiculo v;
        try {
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("SELECT * FROM veiculo WHERE cod_cliente='"+ codCli + "'");
            while (rs.next()) {
                v = new Veiculo(rs.getString("placa"), rs.getString("modelo"),
                        rs.getString("cor"), rs.getString("status_veiculo"));
                veiculos.add(v);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return veiculos;
    }

    public void closeConnection() {
        try {
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
